package net.homelinux.chaoswg.io.renga.krypto.animations.Add;

import net.homelinux.chaoswg.io.renga.core.animations.Animation;
import net.homelinux.chaoswg.io.renga.core.animations.State;

public class AddAnimation extends Animation {
    AddState initialState;
    AddStateChanger addChanger;

    public AddAnimation () {
        initialize(new AddState ());
    }

    public AddAnimation (AddState initialState) {
        initialize(initialState);
    }

    private void initialize(State initialState) throws IllegalArgumentException {
        if (!(initialState instanceof AddState)) {
            throw new IllegalArgumentException ();
        }
        this.initialState = ((AddState)initialState);
        this.addChanger = new AddStateChanger (this.initialState);
    }

    @Override
    public synchronized void start() {
        Thread t = new Thread (addChanger);
        t.setDaemon(true);
        t.start();
        
        try {
            t.join();
        } catch (InterruptedException iexcp){ 
            iexcp.printStackTrace();
        }
        addChanger.close ();
        finished = true;
        setChanged();
        notifyObservers();
    }

    @Override
    public void setOperand(int i, Object operand) {
        initialState.setOperand(i, operand);
    }
}
